import axios from "axios";
import config from "@/config/index";
const token = config.TOKEN;

class HttpRequest {
  constructor(baseUrl) {
    this.baseUrl = baseUrl;
  }
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        // token
        token,
      },
    };
    return config;
  }
  interceptors(instance) {
    // 响应拦截
    instance.interceptors.response.use(
      res => {
        const { data } = res;
        if (data.status !== 1) {
          return Promise.reject(data.error);
        }
        return data;
      },
      error => {
        return Promise.reject(error);
      }
    );
  }
  request(options) {
    const instance = axios.create({
      timeout: 10000,
    });
    options = Object.assign(this.getInsideConfig(), options);
    this.interceptors(instance);
    return instance(options);
  }
}
export default HttpRequest;
